package com.glory.dgcpcpda.activity.cpc;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class DaemonProUpdateActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;

    @BindView(R.id.etEqpWorkID)
    FreeUI_ClearEditText etEqpWorkID;
    @BindView(R.id.etEqpWorkPosition)
    TextView etEqpWorkPosition;
    @BindView(R.id.flEqpTypeList)
    FrameLayout flEqpTypeList;
    @BindView(R.id.ivEqpTypeArrow)
    ImageView ivEqpTypeArrow;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;

    @BindView(R.id.llMain)
    LinearLayout llMain;

    private FreeUI_GeneralPop mEqpTypePop;
    private List<String> mEqpType = new ArrayList<>();
    private EqpInfoEntity eqoinfo;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_daemonupdate;
    }

    @Override
    protected void initView() {
        tvTitle.setText("测试Daemon更新");
        setMainLayout(llMain);
        initEdit(etEqpWorkID, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {
                LotUtils.searchEqpInfo(s, DaemonProUpdateActivity.this, getSupportFragmentManager(), new LotUtils.netCallback<EqpInfoEntity>() {
                    @Override
                    public void onSuccess(EqpInfoEntity response) {
                        etEqpWorkPosition.setText(response.getEQUIPMENT().getLOCATION() + " - " + response.getEQUIPMENT().getEQPTYPE());
                        eqoinfo = response;
                        tvEqpType.setText("");
                        requestEqpList();
                    }

                    @Override
                    public void onFail(Throwable t) {
                        loadError(t,"EQPQUARY");
                    }
                });
            }
        });
        etEqpWorkID.requestFocus();
    }

    @Override
    protected void initData() {
        eqoinfo = new EqpInfoEntity();
    }

    private void requestEqpList() {
        FreeUI_EntityCallBack<BaseResponseBean<List<String>>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<List<String>>>
                (new TypeToken<BaseResponseBean<List<String>>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<List<String>>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    List<String> list = new ArrayList<>();
                    for (String eqp :response.body().getResponse().getBody()) {
                        if(eqp.contains(eqoinfo.getEQUIPMENT().getEQPTYPE())){
                            list.add(eqp);
                        }
                    }
                    mEqpType = list;
                    mEqpTypePop = null;  // 重置菜单
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<List<String>>> response) {
                super.onError(response);
                loadError(response.getException(), "DaemonUpdateType");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ActionType","Query");
        NetUtils.requestPostCPC("DaemonUpdateType", "DaemonUpdateType", this, map, callBack);
    }

    @OnClick({R.id.tvBaseSubmit, R.id.flEqpTypeList})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                String eqpid = etEqpWorkID.getText().toString();
                String eqptype = tvEqpType.getText().toString();

                if (eqpid.equals("") || eqptype.equals("")){
                    showToast("设备编号或者更新类型不能为空",false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean> callBack = new CustomDialogEntityCallBack<BaseResponseBean>
                                (new TypeToken<BaseResponseBean>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean> response) {
                                super.onSuccess(response);
                                showConfirmDialog(response.body().getResponse().getHeader().getRESULTMESSAGE());
                                etEqpWorkID.setText("");
                                etEqpWorkPosition.setText("");
                                tvEqpType.setText("");
                                etEqpWorkID.requestFocus();
                            }

                            @Override
                            public void onError(Response<BaseResponseBean> response) {
                                super.onError(response);
                                loadError(response.getException(),"DaemonUpdateType");
                            }
                        };

                        JsonObject map = new JsonObject();
                        map.addProperty("ActionType","Update");
                        map.addProperty("Equipmentid",eqpid);
                        map.addProperty("UpdateType",eqptype);
                        NetUtils.requestPostCPC("CPC.DaemonUpdateType","DaemonUpdateType",this,map,callBack);
                    }
                },"确认要更新吗？");
                break;

            case R.id.flEqpTypeList:
                if (mEqpType.size() <= 0) {
                    showToast("未获取到更新类型列表", false);
                    return;
                }

                if (mEqpTypePop != null) {
                    if (mEqpTypePop.isShowing()) {
                        mEqpTypePop.dismiss();
                    }
                } else {
                    mEqpTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flEqpTypeList.getWidth(), mEqpType, ivEqpTypeArrow, (codeS, i) -> {
                        tvEqpType.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivEqpTypeArrow);
                mEqpTypePop.showAsDropDown(flEqpTypeList, 0, 0);
                break;
        }

    }

}
